/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pb138.XmlWebServlets;

import cz.muni.fi.pb138.XmlDataBase.Medium;
import cz.muni.fi.pb138.XmlDataBase.Movie;
import java.util.*;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Tomas
 */
public class MediumForm {

    private String id;
    private String name;
    private String type;
    private Set<Movie> movies;

    /**
     * Extract informations from request (webpage), medium name, medium type,
     * movie title, length, length, languages, genre, format and note.
     * 
     * @param request
     * @return MedimForm
     */
    public static MediumForm extractFromRequest(HttpServletRequest request) {
        MediumForm mediumForm = new MediumForm();
        Set<Movie> movies = new HashSet<Movie>();        
        int i = 1;        
        List<String> col = new ArrayList<String>();
        mediumForm.setName(request.getParameter("name"));
        mediumForm.setType(request.getParameter("type"));
        while(request.getParameter("movieRow"+ i + "Title") !=null){
            String title = request.getParameter("movieRow" + i + "Title");
            String length = request.getParameter("movieRow" + i + "Length");
            
            for (int j = 1; j < 5; j++) {
                String language = request.getParameter("movieRow" + i + "Language" + j);
                if(language != null)col.add(language);                
            }            
            
            String genre = request.getParameter("movieRow" + i + "Genre");                        
            String format = request.getParameter("movieRow" + i + "Format");
            String note = request.getParameter("movieRow" + i + "Note");
            Movie movie = new Movie(title,length,note, format,genre,col);
            System.out.println(movie);
            movies.add(movie);
            i++;
        }
        mediumForm.setMovies(movies);
        return mediumForm;
    }

    public Medium validateAndToMedium(StringBuilder errors) {
        String mediumType = stringToType(type, errors);
        Medium medium = new Medium(null, name, mediumType, movies);        
        if (errors.length() > 0) {
            return null;
        }
        return medium;
    }

    private static String stringToType(String value, StringBuilder errors) {
        if (value == null || value.trim().isEmpty()) {
            errors.append("Field 'type' is not filled <br/>");
            return null;
        }

        if (value.equals("CD") || value.equals("DVD") || value.equals("HDD")) {            
            return value;
        }

        errors.append("Field 'Type' has wrong format. <br/>");
        return null;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Set<Movie> getMovies() {
        return movies;
    }

    public void setMovies(Set<Movie> movies) {
        this.movies = movies;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}
